package com.shanyu.nicefish.config;

import com.shanyu.nicefish.common.JsonResult;
import com.shanyu.nicefish.service.HrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import com.alibaba.fastjson.JSON;


/**
 * 登录失败
 *
 * @blame czj Team
 */
public class LoginFailHandler implements AuthenticationFailureHandler {

    @Autowired
    private HrService hrService;

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
                                        AuthenticationException exception) throws IOException, ServletException {
        System.out.println("登陆失败！");
        response.setContentType("application/json;charset=UTF-8");

        response.getWriter().write(JSON.toJSONString(JsonResult.fail(401, "登陆失败：" + exception.getMessage())));

        //new JsonResult() JSONResult.fillResultString(401, "登陆失败：" + exception.getMessage(), null)
    }

}
